﻿using DX;
using DX.IO;
using DX.ViewModels;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Tools.Views;

namespace Tools.ViewModels
{
    [ViewType(typeof(MainWindow))]
    public partial class MainWindowViewModel : ViewModel
    {

        public MainWindowViewModel(IFileSystemDialog fileSystemDialog, IMessageCenter messageCenter)
        {
            this.fileSystemDialog = fileSystemDialog;
            this.messageCenter = messageCenter;
            people = new List<Person>
            {
                new Person("Neil", "Armstrong"),
                new Person("Buzz", "Lightyear"),
                new Person("James", "Kirk")
            };
        }
        private readonly IFileSystemDialog fileSystemDialog;
        private readonly IMessageCenter messageCenter;

        [CommunityToolkit.Mvvm.ComponentModel.ObservableProperty]
        private IList<Person> people;
        public string Greeting { get; } = "(Tools MVVM)Welcome to Avalonia!";
        protected override Task<bool> OnCanClose()
        {
            return messageCenter.Confirmation("是否退出");
        }

        [CommunityToolkit.Mvvm.Input.RelayCommand]
        private async Task Example()
        { 
            var fileEntry = await fileSystemDialog.OpenSingleFile("*|*.txt");
            if (fileEntry == null)
            {
                await messageCenter.Show("未选择文件");
                return;
            }
            var alltext = fileEntry.ReadAllText();
            await messageCenter.Notification(alltext);
        }
    }
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Person(string firstName, string lastName)
        {
            FirstName = firstName;
            LastName = lastName;
        }
    }
}
